import string
from decimal import Decimal
from enum import Enum

#创建一个枚举标记订单的状态
class OrderStatus(Enum):
    # 创建状态
    CREATED = 0
    # 接受状态
    ACCEPTED = 1
    # 部分成交状态
    PARTIALLY_FILLED = 2
    # 完全成交状态
    FILLED = 3
    # 取消状态
    CANCELLED = 4



class StockOrder:
    def __init__(self, order_id:int, stock_symbol:string, quantity:int, price:Decimal, isbuy_order:bool = True, 
                 create_time:int = 0) -> None:
        self.__order_id:int = order_id
        self.__stock_symbol:string = stock_symbol
        self.__quantity:int = quantity
        self.__price:Decimal = price
        self.__status:OrderStatus = OrderStatus.CREATED
        self.__filled_quantity:int = 0
        self.__filled_cost:int = 0
        self.__isbuy_order:bool = isbuy_order 
        self.__create_time:int = create_time  # 订单创建时间，单位为纳秒
    
    def get_order_id(self) -> int:
        return self.__order_id

    def get_stock_symbol(self) -> string:
        return self.__stock_symbol

    def get_quantity(self) -> int:
        return self.__quantity
    
    def get_unfilled_quantity(self) -> int:
        return self.__quantity - self.__filled_quantity

    def get_price(self) -> Decimal:
        return self.__price
    
    def get_status(self) -> OrderStatus:
        return self.__status
    
    def get_filled_quantity(self) -> int:
        return self.__filled_quantity
    
    def get_filled_cost(self) -> int:
        return self.__filled_cost
    
    def is_buy_order(self) -> bool:
        return self.__isbuy_order
    
    def get_create_time(self) -> int:
        return self.__create_time
    
    # 接受订单
    def accept(self) -> None:
        if self.__status != OrderStatus.CREATED:
            raise ValueError("Order can only be accepted if it is in CREATED status")
        
        self.__status = OrderStatus.ACCEPTED
    
    # 订单成交
    def fill(self, quantity:int, fill_price:Decimal) -> None:
        if quantity > self.__quantity - self.__filled_quantity:
            raise ValueError("Cannot fill more than the remaining quantity")
        
        self.__filled_quantity += quantity
        self.__filled_cost += int(fill_price * quantity)
        
        if self.__filled_quantity == self.__quantity:
            self.__status = OrderStatus.FILLED
        else:
            self.__status = OrderStatus.PARTIALLY_FILLED

    def is_filled(self) -> bool:
        return self.__status == OrderStatus.FILLED
    
    def rest_quantity(self) -> int:
        return self.__quantity - self.__filled_quantity

    # 取消订单
    def cancel(self) -> None:
        if self.__status == OrderStatus.FILLED:
            raise ValueError("Cannot cancel a filled order")
        
        self.__status = OrderStatus.CANCELLED
    
    # 字符串化order对象
    def __str__(self) -> string:
        return (f"Order ID: {self.__order_id}, Stock: {self.__stock_symbol}, "
                f"Quantity: {self.__quantity}, Price: {self.__price}, "
                f"Status: {self.__status.name}, Filled Quantity: {self.__filled_quantity}, "
                f"Filled Cost: {self.__filled_cost}, Is Buy Order: {self.__isbuy_order}")
    


    

    